<?php

class Soul_Template_Html_Form_Datetime extends Soul_Template_Html_Form_Input {

    private $_format = 'Y-m-d';
	private $_allowedTime = FALSE;
	private $_allowedDate = TRUE;
	private $_minDate = NULL;
	private $_maxDate = NULL;
	
    public function __construct() {
        $this->templateName = 'form/datetime';
        $this->addClass('input', 'textbox', 'dateinput');
    }
    
    public function getAllowedTime(){
    	return $this->_allowedTime;
    }
    
    public function setAllowedTime($value){
    	$this->_allowedTime = $value;
    	return $this;
    }
    
    public function getAllowedDate(){
    	return $this->_allowedDate;
    }
    
    public function setAllowedDate($value){
    	$this->_allowedDate = $value;
    	return $this;
    }    
    
    public function getMaxDate(){
    	return $this->_maxDate;
    }
    
    public function setMaxDate($value){
    	$this->_maxDate = $value;
    	return $this;
    }
    
    public function getMinDate(){
    	return $this->_minDate;
    }
    
    public function setMinDate($value){
    	$this->_minDate = $value;
    	return $this;
    }    

    public function getFormat(){
        return $this->_format;
    }

    public function setFormat($value){
        $this->_format = $value;
        return $this;
    }
    
    public function setNow(){
        $this->setValue(date($this->getFormat()));
        return $this;
    }

    protected function renderReadonly() {
        return '<span class="readonly">' . Soul_Language_Factory::getCulture()->formatDate($this->getValue(), $this->getFormat()) . '</span>';
    }

    protected function renderEditable() {
        if (!$this->getEnabled()) {
            $this->addClass('disabled');
        }
        $this->setAttribute('type', 'text')
                ->setAttribute('id', $this->getId())
                ->setAttribute('name', $this->getName())
                ->setAttribute('readonly', 'readonly');
        if($this->getValue()){
            $this->setAttribute('value', Soul_Language_Factory::getCulture()->formatDate($this->getValue(), $this->getFormat()));
        }
        if (!$this->getEnabled()) {
            $this->setAttribute('disabled', 'disabled');
        }
        $result = '<input ' . $this->getAttributeHtml() . '/>';
        if($this->getAllowedTime() && $this->getAllowedDate()){
        	$result .= '<script language="javascript">$("#'.$this->getId().'").datetimepicker();</script>';
        }elseif($this->getAllowedTime()){
        	$result .= '<script language="javascript">$("#'.$this->getId().'").timepicker();</script>';
        }elseif($this->getAllowedDate()){
        	$setting = '';
        	if($this->getMaxDate()){
        		$setting .= "maxDate:'".$this->getMaxDate()."'";
        	}
            if($this->getMinDate()){
        		$setting .= "minDate:'".$this->getMinDate()."'";
        	}
        	if(!empty($setting)){
        		$setting = '{'.$setting.'}';
        	}
        	$result .= '<script language="javascript">$("#'.$this->getId().'").datepicker('.$setting.');</script>';
        }
        return $result;
    }

}